// pages/register/register.js
var app = getApp();
var url = app.globalData.url;
var headers;
var waitforcode;
var md = require('../../utils/md5.js');
var Trim = require('../../utils/util.js');
var rxPhone = /^1[3|4|5|7|8][0-9]{9}$/;
var origin = null;//判断来源决定调my还是tologin
var thisflag=true;
Page({
  data:{
    info:{
      tel:"",
      icode:"",
      pwd:"",
      rpwd:"",
      isok:false, 
    },
    i:60,
    waitforcode:false,
  },
  ident:function(e){
    var kdata = this.data.info;    
    kdata[e.target.dataset.i]=e.detail.value;
    // console.log(kdata) ;
    var ok =Boolean(kdata.tel&&kdata.icode&&kdata.pwd&&kdata.rpwd); 
    if(ok){
      kdata.isok=true;
    }else{
      kdata.isok=false;
    }
    this.setData({
      info:kdata
    })
    console.log(this.data.info)
  },
  waitForCode:function(){
    var that=this
    waitforcode =true;
    this.setData({
      waitforcode: waitforcode
    })
    if (waitforcode){
        var i = 60;
        var timer = setInterval(function () {
          console.log(i)
          i--
          that.setData({
            i:i
          })
          if (i<0) {
            waitforcode =false;
            that.setData({
              waitforcode:waitforcode,     
            })
            clearInterval(timer)
          }
        }, 1000)
      }
  },
  register:function(){   
      var info=this.data.info;
      var head = app.globalData.headers;
      if (!Trim.trim(info.tel)) {
        this.showMode("手机号不可为空")
        return;
      } else if (!Trim.trim(info.pwd)) {
        this.showMode("密码不可为空")
        return;
      } else if (Trim.trim(info.pwd) != Trim.trim(info.rpwd)) {
        this.showMode("两次密码不一致")
        return;
      } else if (!thisflag){
        this.showMode("信息提交中请耐心等待")
        return;
      }
      
      if (info.isok){
        thisflag = false;
        wx.request({
          url: url+'api/register',
          method:"POST",
          header: headers,
          data:{
            "phone_num": Trim.trim(info.tel),
            "verification_code": Trim.trim(info.icode),
            "password": md.md5(md.md5(Trim.trim(info.pwd)))
          },
          success:function(data){ 
            thisflag=true;          
            console.log(data)
            let screen = app.globalData.headers.Screen;
            let os = app.globalData.headers.Os;
            if (data.data.result == 1) {
              wx.showModal({
                title: "注册信息",
                content: "注册成功",
                showCancel: false,
                complete: function (res) {
                  if (res.confirm) {
                    app.globalData.headers = {
                      "Content-Type": "application/x-www-form-urlencoded",
                      "Device-Id": "",
                      "Os": os,
                      "Screen": screen,
                      "User-Token": data.data.data.token
                    };
                    try {
                      wx.setStorageSync('infor', JSON.stringify(data.data.data))
                    } catch (e) {
                    };
                    if (origin) {//判断是否要跳到tologin
                      wx.redirectTo({
                        url: '../toLogin/toLogin',
                      });
                    } else {
                      wx.switchTab({
                        url: '../../pages/my/my',
                      });
                    }
                    // wx.switchTab({
                    //   url: '../../pages/index/index',
                    // })
                  }
                }
              })
            } else {
              wx.showModal({
                title: "注册信息",
                content: data.data.message,
                showCancel: false,
                complete: function (res) {
                  if (res.confirm) {

                  }
                }
              })
            }
          }
        })
      }
  },
  showMode: function (mes) {
    wx.showModal({
      title: '来自伴行的提示',
      content: mes,
      showCancel: false,
    });
  },
  getcode(e){
    var that=this;
    if (!waitforcode){
      if (!Trim.trim(that.data.info.tel)){
          this.showMode('请输入手机号')
          return
      } else if (!rxPhone.test(Trim.trim(that.data.info.tel))) {
        this.showMode('手机号码有误')
        return;
      }
      wx.request({
        url: url + 'api/get_sms_verify_code',
        data: {
          "phone_num": "" + Trim.trim(that.data.info.tel) + "",
          "types": "register"
        },
        success: function (data) {
          if (data.data.result === 1) {
            that.waitForCode();
            wx.showModal({
              title: '注册信息',
              content: "验证码已发送，请注意查收",
              showCancel: false,
            })   
          } else {
            wx.showModal({
              title: '注册信息',
              content: data.data.message,
              showCancel: false,
            })
          }
        }
      })
    }
    // return;
    // var flag = this.data.waitforcode;
    // wx.setStorage({
    //   key: 'user_info',
    //   data: this.data.info,
    //   success: function(res){
    //    wx.showModal({
    //       title: '提示消息',
    //       content: '用户信息保存成功',
    //       success: function(res) {
    //         if (res.confirm) {
    //           console.log('用户点击确定')
    //         } else if (res.cancel) {
    //           console.log('用户点击取消')
    //         }
    //       }
    //     })
    //   },
    //   fail: function() {
    //     // fail
    //   },
    //   complete: function() {
    //     // complete
    //   }
    // })
    // console.log(this.data.waitforcode)
    // var that = this;
    // if(!flag){
    //   this.setData({
    //     waitforcode:true
    //   })
    //   let i = 9
    // let timer =  setInterval(function(){
    //     i--;
    //     if(i<0){
    //       clearInterval(timer)
    //       i = 9;
    //     }
    //     that.setData({
    //       i:i
    //     })
    //   },1000)
    //   setTimeout(function(){
    //     that.setData({
    //       waitforcode:false
    //     })
    //   },10000)
    // }else{
    //   return
    // }
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    if (options.from) {
      origin = true
    }
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
    headers = app.globalData.loginheader
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  }
})